/*
Copyright (c) 2011, Carsten Kolve.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the names of PointAttribViz
nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GR_POINTATTRIBVIZHOOK_H_
#define GR_POINTATTRIBVIZHOOK_H_

// Houdini
#include <GR/GR_Detail.h>
#include <GR/GR_RenderHook.h>
#include <GR/GR_DisplayOption.h>
#include <GEO/GEO_AttributeHandle.h>
#include "SOP_PointAttribViz.h"

/// render hook to display PointAttribViz detail data

class GR_PointAttribVizHook : public GR_RenderHook
{
	public:
	/// ctor
	GR_PointAttribVizHook();
	/// dtor
        virtual ~GR_PointAttribVizHook();

        /// Tell Houdini if a particular detail should be rendered using this render hook.
        virtual int getWireMask( GU_Detail *gdp, const GR_DisplayOption *dopt ) const;
        virtual int getShadedMask( GU_Detail *gdp, const GR_DisplayOption *dopt ) const;

        /// Renders the PointAttribs in wireframe
        virtual void renderWire( GU_Detail *gdp,
                    RE_Render &ren,
                    const GR_AttribOffset &ptinfo,
                    const GR_DisplayOption *dopt,
                    float lod,
                    const GU_PrimGroupClosure *hidden_geometry );

        /// Renders the PointAttribs in shaded
        virtual void renderShaded( GU_Detail *gdp,
                    RE_Render &ren,
                    const GR_AttribOffset &ptinfo,
                    const GR_DisplayOption *dopt,
                    float lod,
                    const GU_PrimGroupClosure *hidden_geometry );

        /// Tells Houdini what our render hook is called.
        virtual const char *getName() const
        {
        	return "GR_PointAttribVizHook";
        }

       	virtual GR_HookRender getHookRenderType(GU_Detail * /*gdp*/,
                                                const GR_DisplayOption * /*dopt*/)
        {
        	return VIEW_INDEPENDENT;
        }

        private:
	/// draw our actual data
        void drawPointAttribData( GU_Detail * gdp, 
				RE_Render &ren,
				const GR_DisplayOption *dopt, 
				bool shaded = false);

	/// retrieve a geo attribute from our sop
	int getGeoPointAttribHandle( GU_Detail * gdp, 
				int useAttrib, 
				UT_String &attribStr, 
				GEO_AttributeHandle &attribHandle);
	
	/// value fitting functions
	bool checkFitRange(const float oldMin, const float oldMax, const float newMin,const float newMax, UT_String &errorStr);
	float fit(const float num,const float oldMin,const float oldMax,const float newMin,const float newMax);
	float fitFaster(const float num,const float oldMin,const float oldRange,const float newMin,const float newRange);
	float fitEvenFaster(const float num, const float oldMin, float newMin,const float rangeRatio);

	SOP_PointAttribViz *pointAttribViz_sop;
	void raisePointAttribVizError(const UT_String &strA, const UT_String &strB, const UT_String &strC);

};


#endif /* GR_POINTATTRIBVIZHOOK_H_ */
